import sys
import os

file_path = os.path.abspath(__file__)
end = file_path.index('qmt_auto_trade') + 17
project_path = file_path[0:end]
sys.path.append(project_path)

import configparser
from pathlib import Path
import ast


def get_config_info():
    # 获取当前脚本的绝对路径
    current_dir = Path(__file__).parent.resolve()

    # 在多个可能的位置查找配置文件
    possible_paths = [
        current_dir / "config_info.ini",  # 同一目录
        current_dir.parent / "config_info.ini",  # 上级目录
        current_dir / "config" / "config_info.ini",  # 子目录
        Path.home() / ".myapp" / "config_info.ini",  # 用户目录
    ]
    config = configparser.ConfigParser()

    # 尝试读取配置文件
    found = False
    for path in possible_paths:
        if path.exists():
            config.read(path, encoding='utf-8')
            found = True
            break

    if not found:
        raise FileNotFoundError("未找到配置文件")

    qmt_account_no = config.getint("account", "qmt_account_no")
    qmt_account_password = config.getint("account", "qmt_account_password")
    qmt_exe_path = config.get("account", "qmt_exe_path")

    qmt_data_path = config.get("account", "qmt_data_path")
    wx_pusher_token = config.get("account", "wx_pusher_token")
    wx_pusher_uid = config.get("account", "wx_pusher_uid")

    email_sender_name = config.get("account", "email_sender_name")
    sender_email = config.get("account", "sender_email")
    email_token = config.get("account", "email_token")
    receiver_emails = config.get("account", "receiver_emails")
    # 转换为列表
    receiver_emails = ast.literal_eval(receiver_emails)

    smtp_server = config.get("account", "smtp_server")
    smtp_port = config.get("account", "smtp_port")

    strategy_interval_seconds = config.getint("strategy", "strategy_interval_seconds")
    strategy_template_path = config.get("strategy", "strategy_template_path")
    strategy_trade_money = config.getfloat("strategy", "strategy_trade_money")
    max_buy_chg_float = config.getfloat('strategy', 'max_buy_chg_float')
    am_end_time = config.get('strategy', 'am_end_time')
    pm_end_time = config.get('strategy', 'pm_end_time')

    dominant_theme_count_min = config.getfloat('strategy', 'dominant_theme_count_min')
    dominant_theme_count_max = config.getfloat('strategy', 'dominant_theme_count_max')

    extra_col_16_min = config.getfloat('strategy', 'extra_col_16_min')
    extra_col_16_min = round(extra_col_16_min / 100, 10)
    extra_col_16_max = config.getfloat('strategy', 'extra_col_16_max')
    extra_col_16_max = round(extra_col_16_max / 100, 10)
    extra_col_17_min = config.getfloat('strategy', 'extra_col_17_min')
    extra_col_17_min = round(extra_col_17_min / 100, 10)
    extra_col_17_max = config.getfloat('strategy', 'extra_col_17_max')
    extra_col_17_max = round(extra_col_17_max / 100, 10)
    extra_col_18_min = config.getfloat('strategy', 'extra_col_18_min')
    extra_col_18_min = round(extra_col_18_min / 100, 10)
    extra_col_18_max = config.getfloat('strategy', 'extra_col_18_max')
    extra_col_18_max = round(extra_col_18_max / 100, 10)

    trader_db_name = config.get("db_name", "trader_db_name")
    stock_trade_record = config.get("db_name", "stock_trade_record")
    trade_date_list = config.get("db_name", "trade_date_list")
    self_position = config.get("db_name", "self_position")

    result_dict = {
        'email_sender_name': str(email_sender_name),
        'sender_email': str(sender_email),
        'email_token': str(email_token),
        'receiver_emails': receiver_emails,
        'smtp_server': str(smtp_server),
        'smtp_port': int(smtp_port),

        'qmt_account_no': str(qmt_account_no),
        'qmt_account_password': str(qmt_account_password),
        'qmt_exe_path': qmt_exe_path,
        'qmt_data_path': qmt_data_path,
        'wx_pusher_token': wx_pusher_token,
        'wx_pusher_uid': wx_pusher_uid,

        'strategy_interval_seconds': strategy_interval_seconds,
        'strategy_template_path': strategy_template_path,
        'strategy_trade_money': strategy_trade_money,
        'max_buy_chg_float': max_buy_chg_float,
        'am_end_time': am_end_time,
        'pm_end_time': pm_end_time,

        'dominant_theme_count_min': dominant_theme_count_min,
        'dominant_theme_count_max': dominant_theme_count_max,
        'extra_col_16_min': extra_col_16_min,
        'extra_col_16_max': extra_col_16_max,

        'extra_col_17_min': extra_col_17_min,
        'extra_col_17_max': extra_col_17_max,

        'extra_col_18_min': extra_col_18_min,
        'extra_col_18_max': extra_col_18_max,

        'trader_db_name': trader_db_name,
        'stock_trade_record': stock_trade_record,
        'self_position': self_position,
        'trade_date_list': trade_date_list,
    }
    return result_dict


if __name__ == '__main__':
    get_config_info()
